/********************************************\
 * Edited from
\********************************************/

package com.adobe.mx.containers

{

import flash.events.Event;
import flash.events.FocusEvent;
import flash.events.MouseEvent;

import mx.containers.Panel;
import mx.controls.TextInput;
import mx.core.Application;
import mx.core.UIComponent;
import mx.core.UITextField;
import mx.events.FlexEvent;


public class EditablePanel extends Panel

{

public var editableTitle:TextInput;

private var pTitleBar:UIComponent = new UIComponent();

[Bindable]
private var editing:Boolean = false;

public var marker:UITextField;

public function EditablePanel():void
{

}

protected override function createChildren():void{

	super.createChildren();
	
	super.titleTextField.autoSize = "left";
	
	super.titleTextField.doubleClickEnabled = true;
	
	
	
	pTitleBar = super.titleBar;
		
	editableTitle = new TextInput();
	
	editableTitle.width = 150;
	
	editableTitle.height = 20;
	
	editableTitle.enabled = true;
	
	editableTitle.visible = editing;
	
	marker = new UITextField();
	
	marker.y = 5;
	
	marker.setColor(0xFFFFFF);
		
	pTitleBar.addChild(editableTitle);
	
	pTitleBar.addChild(marker);
	
	initListeners();

}

private function editTitle(event:MouseEvent):void{
	editing = true;
	editableTitle.visible = editing;
	editableTitle.text = super.title;
	editableTitle.setFocus();
	
	updateTitle(event);
	
	editableTitle.addEventListener(Event.CHANGE, updateTitle);
	editableTitle.addEventListener(FocusEvent.FOCUS_OUT, hideEditableTitle);
	editableTitle.addEventListener(FlexEvent.ENTER, hideEditableTitle);	
	Application.application.addEventListener(MouseEvent.MOUSE_DOWN, checkFocus);
	
}

private function hideEditableTitle(event:*):void{
	editing = false;
	editableTitle.visible = editing;
	super.titleTextField.text = super.title = editableTitle.text;
	
	invalidateDisplayList(); 
	
	editableTitle.removeEventListener(Event.CHANGE, updateTitle);
	editableTitle.removeEventListener(FocusEvent.FOCUS_OUT, hideEditableTitle);
	editableTitle.removeEventListener(FlexEvent.ENTER, hideEditableTitle);
	Application.application.removeEventListener(MouseEvent.MOUSE_DOWN, checkFocus);
}

protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
	super.updateDisplayList(unscaledWidth, unscaledHeight);
	editableTitle.x = super.titleTextField.x;
	editableTitle.y = super.titleTextField.y;
	marker.x = super.titleTextField.x + super.titleTextField.textWidth + 2;
	editableTitle.visible = editing;
}

public function initListeners():void{
	super.titleTextField.addEventListener(MouseEvent.DOUBLE_CLICK, editTitle);	
}


public function updateTitle(event:Event):void{
	super.title = "";
	super.titleTextField.text = "";
	invalidateDisplayList();
}

public function checkFocus(event:MouseEvent):void{
	if (!(event.target is UITextField)) hideEditableTitle(event);	
}
}
}

